document.write('<script src="' + context
		+ '/app/gdzc/js/itemoptions.js"></script>');

function editPage(id) {
	var tb = new Ext.Toolbar();

	tb.id = 'ExtToolBar';

	var saveAction = new Ext.Action({
				id : 'saveButton',
				text : '<span style="font-size: 13px" title="提交保存模板定义">提交保存</font>',
				handler : function() {
					remixTable('APP_GDZC_TEMPLATE,APP_GDZC_TEMPITEM',
							'single,multi', '-1,itemCount', function() {
								return checkit();
							}, function() {
								forwardFunc();
							});
				},
				iconCls : 'save'
			});

	var backAction = new Ext.Action({
		id : 'backAction',
		text : '<span style="font-size: 13px" title="返回模板列表">返回</font>',
		handler : function() {
			document.location = context
					+ '/system/tablequery/jsplist.jsp?queryKey=gdzctemplist&typeId='
					+ $('TYPEID').value;
		},
		iconCls : 'back'
	});

	tb.add(saveAction);
	tb.add(backAction);
	// tb.add('-');

	tb.render('toolbar');

	init();

	initTableI('APP_GDZC_TEMPITEM', id, 'TEMPID', 'multi', function(json) {
				editBasicItems(json);
			});
}

/**
 * 编辑模板项
 * 
 * @param {}
 *            json
 */
function editBasicItems(json) {
	for (var i = 0; i < json.list.length; i++) {
		if (json.list[i].PROPKEY == 'COMMON') {
			getCommonModelValue(json.list[i], '5', json.list[i].PROPKEY);
		} else if (json.list[i].PROPKEY == 'BASE') {
			getModelValue(json.list[i], '1', json.list[i].PROPKEY);
		} else if (json.list[i].PROPKEY == 'CONFIG') {
			getModelValue(json.list[i], '2', json.list[i].PROPKEY);
		} else if (json.list[i].PROPKEY == 'FINANCE') {
			getModelValue(json.list[i], '3', json.list[i].PROPKEY);
		} else if (json.list[i].PROPKEY == 'MAINTEN') {
			getModelValue(json.list[i], '4', json.list[i].PROPKEY);
		}
	}

}

function checkit() {
	var rnt = false;

	if (isblank($('TNAME'))) {
		alert('请填写模板名称');
	} else {
		var itemCount = getItemCount();

		createHidden('itemCount', itemCount);
		// alert('itemCount==='+itemCount);

		createHidden('USERID', userId);
		createHidden('USERNAME', userName);
		createHidden('GROUPID', groupId);
		createHidden('GROUPNAME', groupName);
		createHidden('TDATE', nowTime());

		rnt = true;
	}

	return rnt;
}

/**
 * 保存成功后回调函数
 */
function forwardFunc() {
	alert('模板保存成功');
	document.location = context
			+ '/system/tablequery/jsplist.jsp?queryKey=gdzctemplist&typeId='
			+ $('TYPEID').value;
}

/**
 * 
 * @param {}
 *            o
 */
function getItemCount() {
	var cnt = '';

	var cnt1 = $('DragContainer1').childNodes;
	var cnt2 = $('DragContainer2').childNodes;
	var cnt3 = $('DragContainer3').childNodes;
	var cnt4 = $('DragContainer4').childNodes;
	var cnt5 = $('DragContainer5').childNodes;

	for (var i = 0; i < cnt1.length; i++) {
		var tb = cnt1[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt2.length; i++) {
		var tb = cnt2[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt3.length; i++) {
		var tb = cnt3[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt4.length; i++) {
		var tb = cnt4[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	for (var i = 0; i < cnt5.length; i++) {
		var tb = cnt5[i];
		var idx = tb.getAttribute('_id');

		cnt += idx + ',';
	}

	return cnt;
}

/**
 * 详细设置模板项
 * 
 * @param {}
 *            itemid
 */
function editItem(itemid, lbl) {
	var form = new Ext.FormPanel({
		labelWidth : 100, // label settings here cascade unless
		frame : false,
		bodyStyle : 'border:0px; padding:5px;',
		width : 450,
		defaults : {
			width : 300
		},
		defaultType : 'textfield',

		items : [{
					fieldLabel : '资产属性',
					name : 'tprop',
					readOnly : true,
					value : lbl
				}, {
					fieldLabel : '关联字段',
					name : 'cname',
					readOnly : true,
					value : $(itemid).getAttribute('columnName')
				}, {
					fieldLabel : '字段类型',
					name : 'ctype',
					readOnly : true,
					value : $(itemid).getAttribute('columnType')
				}, {
					fieldLabel : '关联数据表',
					name : 'tname',
					readOnly : true,
					value : $(itemid).getAttribute('tableName')
				}, {
					fieldLabel : '备注内容',
					xtype : 'textarea',
					name : 'tmemo'
				}, {
					fieldLabel : '特殊参数',
					xtype : 'textarea',
					name : 'tparam'
				}, new Ext.form.CheckboxGroup({
							fieldLabel : '值校验',
							// //RadioGroup.fieldLabel 标签与
							// Radio.boxLabel 标签区别
							// width : "200",
							// hideLabel : true, // 隐藏RadioGroup标签
							items : [new Ext.form.Checkbox({ // 三个必须项
										id : 'tck0',
										// checked : true, //
										// 设置当前为选中状态,仅且一个为选中.
										boxLabel : '非空校验', // Radio标签
										name : 'tck0', // 用于form提交时传送的参数名
										inputValue : '1', // 提交时传送的参数值
										listeners : {
											check : function(checkbox, checked) { // 选中时,调用的事件
												if (checked) {

												}
											}
										}
									}), new Ext.form.Checkbox({ // 以上相同
										id : 'tck1',
										boxLabel : '类型校验',
										name : 'tck1',
										inputValue : '2',
										listeners : {
											check : function(checkbox, checked) {
												if (checked) {

												}
											}
										}
									})]
						})]
	});

	var idx = $(itemid).getAttribute('idx');

	var win = new Ext.Window({
				title : '模板项详细设置',
				layout : 'fit',
				items : [form],
				plain : true,
				modal : true,
				width : 500,
				height : 400,
				listeners : {
					'show' : function() {
						form.getForm().findField('tmemo')
								.setValue($('COLUMNMEMO_' + idx).value);
						form.getForm().findField('tparam')
								.setValue($('COLUMNOTHER_' + idx).value);
						if ($('COLUMNCHK1_' + idx).value == 1) {
							Ext.get('tck0').dom.checked = true
						}

						if ($('COLUMNCHK2_' + idx).value == 1) {
							Ext.get('tck1').dom.checked = true
						}
					}
				},
				buttons : [{
					text : '确定',
					handler : function() {
						$('COLUMNMEMO_' + idx).value = form.getForm()
								.findField('tmemo').getValue();
						$('COLUMNOTHER_' + idx).value = form.getForm()
								.findField('tparam').getValue();

						if (Ext.get('tck0').dom.checked == true) {
							$('COLUMNCHK1_' + idx).value = '1';
						} else {
							$('COLUMNCHK1_' + idx).value = '0';
						}

						if (Ext.get('tck1').dom.checked == true) {
							$('COLUMNCHK2_' + idx).value = '1';
						} else {
							$('COLUMNCHK2_' + idx).value = '0';
						}

						win.close();
						win = null;
					}
				}, {
					text : '取消',
					handler : function() {
						win.close();
						win = null;
					}
				}]
			});

	win.show(this);
}

/**
 * tab 切换
 * 
 * @param {}
 *            name
 * @param {}
 *            cursel
 * @param {}
 *            n
 */
function setTab(name, cursel, n) {
	for (i = 1; i <= n; i++) {
		var menu = document.getElementById(name + i);
		var con = document.getElementById("con_" + name + "_" + i);
		menu.className = i == cursel ? "hover" : "";
		con.style.display = i == cursel ? "block" : "none";
		$('img' + i)
				? ($('img' + i).style.display = i == cursel ? "" : "none")
				: null;
	}
}

/**
 * 获取模板属性内容
 * 
 * @param {}
 *            obj
 * @param {}
 *            model_xh
 */
function getModelValue(obj, model_xh, prop) {
	templateItemMap.put(obj.TABLENNAME + '.' + obj.COLUMNNAME, '1');

	var tn = obj.TABLENNAME;
	var name = obj.COLUMNNAME;
	var memo = obj.COLUMNLABEL;
	var tp = obj.COLUMNTYPE;
	var itemid = tn + '.' + name;
	var chk1 = obj.COLUMNCHK1;
	var chk2 = obj.COLUMNCHK2;
	var dvalue = obj.COLUMNDVALUE;
	var propkey = obj.PROPKEY;
	var proplabel = obj.PROPLABEL;
	var zb_pkid = obj.ITEMID;
	str = '<table width="98%" border="0" align="center" id="' + itemid
			+ '" idx="' + c + '" tn="' + tn + '" tnLabel="' + proplabel
			+ '" columnName="' + name + '" tableName="' + tn + '" columnType="'
			+ tp + '">';
	str += '<tr>';
	str += '<td width="10%" align="center">';
	if (prop != 'COMMON') {
		str += '<img src="'
				+ context
				+ '/app/gdzc/images/remove.png" alt="删除模板项" style="cursor:hand;" onClick="javascript:removeItem1(\'div_'
				+ c + '\', \'' + itemid + '\', \'' + zb_pkid + '\');">';
	}
	str += '&nbsp;<img src="'
			+ context
			+ '/app/gdzc/images/edititem.png" alt="详细设置" style="cursor:hand;" onClick="javascript:editItem(\''
			+ itemid + '\', \'' + proplabel + '\');">';
	str += '</td>';
	str += '<td width="30%" align="left">&nbsp;显示名称<br/>'
	str += '&nbsp;<input style="width:90%" type="text" name="COLUMNLABEL_'
			+ c + '" value="' + memo + '">';
	str += '<input type="hidden" name="PROPKEY_' + c + '" value="' + propkey
			+ '">';
	str += '<input type="hidden" name="PROPLABEL_' + c + '" value="'
			+ proplabel + '">';
	str += '<input type="hidden" name="COLUMNNAME_' + c + '" value="' + name
			+ '">';
	str += '<input type="hidden" name="COLUMNTYPE_' + c + '" value="' + tp
			+ '">';
	str += '<input type="hidden" name="TABLENNAME_' + c + '" value="' + tn
			+ '">';
	str += '<input type="hidden" name="COLUMNMEMO_' + c + '" value="">';
	str += '<input type="hidden" name="COLUMNCHK1_' + c + '" value="' + chk1
			+ '">';
	str += '<input type="hidden" name="COLUMNCHK2_' + c + '" value="' + chk2
			+ '">';
	str += '<input type="hidden" name="COLUMNCHECK_' + c + '" value="">';
	str += '<input type="hidden" name="COLUMNOTHER_' + c + '" value="'
			+ obj.COLUMNOTHER + '">';
	str += '<input type="hidden" name="ITEMID_' + c + '" value="' + zb_pkid
			+ '">';
	str += '</td>';
	str += '<td width="30%" align="left">&nbsp;默认值<br/>';
	str += '&nbsp;<input style="width:90%" type="text" name="COLUMNDVALUE_'
			+ c + '" value="' + dvalue + '">';
	str += '</td>';
	str += '<td width="30%" align="left">&nbsp;类型<br/>';
	str += '&nbsp;<select style="width:95%" name="ITEMTYPE_' + c + '">'
			+ getItemOptions(tn, tp, obj.ITEMTYPE) + '</select>';
	str += '</td>';
	str += '</tr>';
	str += '</table>';

	var o = $('DragContainer' + model_xh);

	var bindObject = document.createElement("div");
	bindObject.id = "div_" + c;
	bindObject.setAttribute("_id", c);
	bindObject.className = "DragBox";

	bindObject.innerHTML = str;

	o.appendChild(bindObject);

	c++;

	CreateDragContainer(o);
}

/**
 * 获取模板属性内容
 * 
 * @param {}
 *            obj
 * @param {}
 *            model_xh
 */
function getCommonModelValue(obj, model_xh, prop) {
	templateItemMap.put(obj.TABLENNAME + '.' + obj.COLUMNNAME, '1');

	var tn = obj.TABLENNAME;
	var name = obj.COLUMNNAME;
	var memo = obj.COLUMNLABEL;
	var tp = obj.COLUMNTYPE;
	var itemid = tn + '.' + name;
	var chk1 = obj.COLUMNCHK1;
	var chk2 = obj.COLUMNCHK2;
	var dvalue = obj.COLUMNDVALUE;
	var propkey = obj.PROPKEY;
	var proplabel = obj.PROPLABEL;
	var zb_pkid = obj.ITEMID;
	str = '<table width="98%" border="0" align="center" id="' + itemid
			+ '" idx="' + c + '" tn="' + tn + '" tnLabel="' + proplabel
			+ '" columnName="' + name + '" tableName="' + tn + '" columnType="'
			+ tp + '">';
	str += '<tr>';
	str += '<td width="10%" align="center">';
	if (prop != 'COMMON') {
		str += '<img src="'
				+ context
				+ '/app/gdzc/images/remove.png" alt="删除模板项" style="cursor:hand;" onClick="javascript:removeItem1(\'div_'
				+ c + '\', \'' + itemid + '\', \'' + zb_pkid + '\');">';
	}
	str += '&nbsp;<img src="'
			+ context
			+ '/app/gdzc/images/edititem.png" alt="详细设置" style="cursor:hand;" onClick="javascript:editItem(\''
			+ itemid + '\', \'' + proplabel + '\');">';
	str += '</td>';
	str += '<td width="30%" align="left">&nbsp;显示名称<br/>'
	str += '&nbsp;<input style="width:90%" type="text" name="COLUMNLABEL_'
			+ c + '" value="' + memo + '">';
	str += '<input type="hidden" name="PROPKEY_' + c + '" value="' + propkey
			+ '">';
	str += '<input type="hidden" name="PROPLABEL_' + c + '" value="'
			+ proplabel + '">';
	str += '<input type="hidden" name="COLUMNNAME_' + c + '" value="' + name
			+ '">';
	str += '<input type="hidden" name="COLUMNTYPE_' + c + '" value="' + tp
			+ '">';
	str += '<input type="hidden" name="TABLENNAME_' + c + '" value="' + tn
			+ '">';
	str += '<input type="hidden" name="COLUMNMEMO_' + c + '" value="">';
	str += '<input type="hidden" name="COLUMNCHK1_' + c + '" value="' + chk1
			+ '">';
	str += '<input type="hidden" name="COLUMNCHK2_' + c + '" value="' + chk2
			+ '">';
	str += '<input type="hidden" name="COLUMNCHECK_' + c + '" value="">';
	str += '<input type="hidden" name="COLUMNOTHER_' + c + '" value="'
			+ obj.COLUMNOTHER + '">';
	str += '<input type="hidden" name="ITEMID_' + c + '" value="' + zb_pkid
			+ '">';
	str += '</td>';
	str += '<td width="30%" align="left">&nbsp;默认值<br/>';
	str += '&nbsp;<input style="width:90%" type="text" name="COLUMNDVALUE_'
			+ c + '" value="' + dvalue + '">';
	str += '</td>';
	str += '<td width="30%" align="left">&nbsp;类型<br/>';
	str += '&nbsp;<select style="width:95%" name="ITEMTYPE_' + c + '">'
			+ getCommonItemOptions(tn, name, tp, obj.ITEMTYPE) + '</select>';
	str += '</td>';
	str += '</tr>';
	str += '</table>';

	var o = $('DragContainer' + model_xh);

	var bindObject = document.createElement("div");
	bindObject.id = "div_" + c;
	bindObject.setAttribute("_id", c);
	bindObject.className = "DragBox";

	bindObject.innerHTML = str;

	o.appendChild(bindObject);

	c++;

	CreateDragContainer(o);
}